/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.order.controller.app.feedback;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.order.controller.app.feedback.vo.FeedbackCreateReqVO;
import co.yixiang.yshop.module.order.controller.app.feedback.vo.FeedbackAnalysisRespVO;
import co.yixiang.yshop.module.order.service.feedback.FeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <p>
 * 订单反馈控制器
 * </p>
 *
 * @author yshop
 * @since 2024-01-01
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "用户 APP - 订单反馈模块")
@RequestMapping("/api/v1/feedback")
@Validated
public class FeedbackController {

    private final FeedbackService feedbackService;

    /**
     * 提交订单反馈
     */
    @PreAuthenticated
    @PostMapping
    @Operation(summary = "提交订单反馈")
    public CommonResult<String> createFeedback(@RequestBody @Valid FeedbackCreateReqVO createReqVO) {
        // 设置当前登录用户ID
        Long currentUserId = getLoginUserId();
        createReqVO.setUserId(currentUserId.toString());

        String feedbackId = feedbackService.createFeedback(createReqVO);
        log.info("[createFeedback][用户({})成功提交反馈，反馈ID：{}]", currentUserId, feedbackId);

        return success(feedbackId);
    }

    /**
     * 获取用户的反馈分析结果列表
     */
    @PreAuthenticated
    @GetMapping("/analysis")
    @Operation(summary = "获取用户的反馈分析结果列表")
    public CommonResult<List<FeedbackAnalysisRespVO>> getUserFeedbackAnalysis() {
        Long currentUserId = getLoginUserId();
        List<FeedbackAnalysisRespVO> analysisResults = feedbackService.getUserFeedbackAnalysis(currentUserId.toString());
        return success(analysisResults);
    }

    /**
     * 根据反馈ID获取分析结果
     */
    @PreAuthenticated
    @GetMapping("/analysis/{feedbackId}")
    @Operation(summary = "根据反馈ID获取分析结果")
    @Parameter(name = "feedbackId", description = "反馈ID", required = true, example = "12345")
    public CommonResult<FeedbackAnalysisRespVO> getFeedbackAnalysis(@PathVariable String feedbackId) {
        Long currentUserId = getLoginUserId();
        FeedbackAnalysisRespVO analysisResult = feedbackService.getFeedbackAnalysis(feedbackId, currentUserId.toString());
        return success(analysisResult);
    }
}